package com.gameShare.controller.user

import com.gameShare.controller.base.LoginVerifyController
import com.gameShare.entity.dto.game.GameCollectionDto
import com.gameShare.entity.dto.user.UserCollectionFolderDto
import com.gameShare.entity.vo.PageVo
import com.gameShare.entity.vo.ResponseVo
import com.gameShare.service.user.UserCollectionService
import jakarta.servlet.http.HttpServletRequest
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("userCollection")
class UserCollectionController(private val userCollectionService: UserCollectionService) : LoginVerifyController() {

    data class CreateCollectionFolderParams(
        val folderName: String,
        @RequestParam(required = false)
        val permission: Int?
    )
    /**
     * 创建收藏夹
     *
     * @param httpServletRequest
     * @param params 接口参数
     * @return
     */
    @PostMapping("createCollectionFolder")
    fun createCollectionFolder(
        httpServletRequest: HttpServletRequest, params : CreateCollectionFolderParams): ResponseVo<Boolean> {
        val userId = getUserInfoByToken(httpServletRequest).id
        return getSuccessResponseVO(
            userCollectionService.createCollectionFolder(params.folderName, userId,params.permission)
        )
    }

    /**
     * 删除收藏夹
     *
     * @param httpServletRequest 请求头
     * @param collectionFolderId 收藏夹id
     * @return
     */
    @PostMapping("delCollectionFolder/{id}")
    fun delCollectionFolder(
        httpServletRequest: HttpServletRequest,
        @PathVariable("id") collectionFolderId: Int
    ): ResponseVo<Boolean> {
        val userId = getUserInfoByToken(httpServletRequest).id
        return getSuccessResponseVO(userCollectionService.delCollectionFolder(userId, collectionFolderId))
    }


    data class ChangeCollectionFolderParams(
        @RequestParam(required = false)
        val name : String?,
        @RequestParam(required = false)
        val permission : Int?,
        val collectionFolderId : Int
    )
    /**
     * 改变收藏夹信息
     *
     * @param httpServletRequest
     * @param params 接口json参数
     * @return
     */
    @PostMapping("changeCollectionFolderInfo")
    fun changeCollectionFolderInfo(
        httpServletRequest: HttpServletRequest,
        params: ChangeCollectionFolderParams
    ): ResponseVo<Boolean> {
        val userId = getUserInfoByToken(httpServletRequest).id
        return getSuccessResponseVO(
            userCollectionService.changeCollectionFolderInfo(
                 params.name,params.permission, userId,params.collectionFolderId
            )
        )
    }


    /**
     * 添加游戏到收藏夹
     *
     * @param httpServletRequest
     * @param gameId 游戏id
     * @param collectionFolderId 收藏夹id
     * @return
     */
    @RequestMapping("addGameToCollection")
    fun addGameToCollection(
        httpServletRequest: HttpServletRequest,
        @RequestParam(required = true) gameId : String,
        @RequestParam(required = true) collectionFolderId : Int
    ): ResponseVo<Boolean> {
        val userId = getUserInfoByToken(httpServletRequest).id
        return getSuccessResponseVO(
            userCollectionService.addGameToCollection(gameId, userId, collectionFolderId)
        )
    }

    data class DelGameInCollectionParam(
        @RequestParam
        val gameId : String,
        @RequestParam
        val collectionFolderId : Int
    )

    // TODO 改为post
    @PostMapping("delGameInCollection")
    fun delGameInCollection(
        httpServletRequest: HttpServletRequest,
        params: DelGameInCollectionParam
    ): ResponseVo<Boolean> {
        val userId = getUserInfoByToken(httpServletRequest).id
        return getSuccessResponseVO(
            userCollectionService.delGameInCollection(
                params.gameId,
                userId,
                params.collectionFolderId)
        )
    }

    /**
     * 加载所有收藏夹
     *
     * @param httpServletRequest
     * @param userId 用户id
     * @return
     */
    @RequestMapping("loadAllCollectionFolder")
    fun loadAllCollectionFolder(
        httpServletRequest: HttpServletRequest,
        @RequestParam(required = true) userId : String
    ): ResponseVo<List<UserCollectionFolderDto>> {
        val visitUserId = getUserInfoByToken(httpServletRequest).id
        return getSuccessResponseVO(
            userCollectionService.loadAllCollectionFolder(userId, visitUserId)
        )
    }

    /**
     *
     * 加载用户收藏夹下所有游戏数据
     * @param httpServletRequest
     * @param pageNo
     * @param pageSize
     * @param collectionFolderId
     * @return
     */
    @RequestMapping("loadGamePageInCollection")
    fun loadGamePageInCollection(
        httpServletRequest: HttpServletRequest,
        pageNo : Int?,
        pageSize : Int?,
        @RequestParam(required = true)  collectionFolderId: Int
    ): ResponseVo<PageVo<GameCollectionDto?>> {
        val userId = getUserInfoByToken(httpServletRequest).id
        return getSuccessResponseVO(
            userCollectionService.loadGamePageInCollection(pageNo, pageSize, userId, collectionFolderId)
        )
    }
}